package com.pyip.reggie.controller;

import com.pyip.reggie.Entity.AddressBook;
import com.pyip.reggie.common.R;
import com.pyip.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/addressBook")
@RestController
@Slf4j
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;


    /**
     * 新增一个地址
     * @param addressBook
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public R<AddressBook> addAddressBook(@RequestBody AddressBook addressBook){
        return addressBookService.addAddressBook(addressBook);
    }

    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @RequestMapping(value = "/default",method = RequestMethod.PUT)
    public R<AddressBook> setDefaultAddressBook(@RequestBody AddressBook addressBook){
        return addressBookService.setDefaultAddressBook(addressBook);
    }

    /**
     * 根据id查找地址
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public R<AddressBook> getAddressById(@PathVariable long id){
        return addressBookService.getById(id) == null ? R.error("没有找到该地址") : R.success(addressBookService.getById(id));
    }

    /**
     * 根据用户id查找默认地址
     * @return
     */
    @RequestMapping(value = "/default",method = RequestMethod.GET)
    public R<AddressBook> getDefault(){
        return addressBookService.getDefault();
    }

    /**
     * 查找用户的全部地址
     * @param addressBook
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public R<List<AddressBook>> getList(AddressBook addressBook){
        return addressBookService.getList(addressBook);
    }
}
